package cn.kpi.common.utils;

import java.text.ParseException;
import java.util.Date;
import java.util.GregorianCalendar;
import org.apache.commons.lang3.time.DateFormatUtils;

public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
	private static String[] parsePatterns = {
			"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
			"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
			"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

		public static String getDate() {
			return getDate("yyyy-MM-dd");
		}

		public static String getDate(String pattern) {
			return DateFormatUtils.format(new Date(), pattern);
		}

		public static String formatDate(Date date, Object... pattern) {
			String formatDate = null;
			if (pattern != null && pattern.length > 0) {
				formatDate = DateFormatUtils.format(date, pattern[0].toString());
			} else {
				formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
			}
			return formatDate;
		}

		public static String formatDateTime(Date date) {
			return formatDate(date, "yyyy-MM-dd HH:mm:ss");
		}

		public static String getTime() {
			return formatDate(new Date(), "HH:mm:ss");
		}
		

		public static String getGregorian() {
			GregorianCalendar ca = new GregorianCalendar(); 
		
			if(ca.get(GregorianCalendar.AM_PM) == 0){
				return "����";
			}else{
				return "����";
			}
		}

		public static String getDateTime() {
			return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
		}


		public static String getYear() {
			return formatDate(new Date(), "yyyy");
		}


		public static String getMonth() {
			return formatDate(new Date(), "MM");
		}


		public static String getDay() {
			return formatDate(new Date(), "dd");
		}

		public static String getWeek() {
			return formatDate(new Date(), "E");
		}

		public static Date parseDate(Object str) {
			if (str == null){
				return null;
			}
			try {
				return parseDate(str.toString(), parsePatterns);
			} catch (ParseException e) {
				return null;
			}
		}

		public static long pastDays(Date date) {
			long t = new Date().getTime()-date.getTime();
			return t/(24*60*60*1000);
		}

		public static long pastHour(Date date) {
			long t = new Date().getTime()-date.getTime();
			return t/(60*60*1000);
		}
		
		public static long pastMinutes(Date date) {
			long t = new Date().getTime()-date.getTime();
			return t/(60*1000);
		}
		
	    public static String formatDateTime(long timeMillis){
			long day = timeMillis/(24*60*60*1000);
			long hour = (timeMillis/(60*60*1000)-day*24);
			long min = ((timeMillis/(60*1000))-day*24*60-hour*60);
			long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60);
			long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000);
			return (day>0?day+",":"")+hour+":"+min+":"+s+"."+sss;
	    }
		public static double getDistanceOfTwoDate(Date before, Date after) {
			long beforeTime = before.getTime();
			long afterTime = after.getTime();
			return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
		}
		public static double getMins(Date before, Date after) {
			long beforeTime = before.getTime();
			long afterTime = after.getTime();
			return (afterTime - beforeTime) / (1000 * 60 );
		}
		public static double getHours(Date before, Date after) {
			long beforeTime = before.getTime();
			long afterTime = after.getTime();
			return (afterTime - beforeTime) / (1000 * 60 * 60);
		}
		

		public static void main(String[] args) throws ParseException {
			
			GregorianCalendar ca = new GregorianCalendar();
			System.out.println(ca.get(GregorianCalendar.AM_PM));
		}
		
	
		public static boolean isOverlap(String startdate1, String enddate1,  
	            String startdate2, String enddate2) {  
	        Date leftStartDate = null;  
	        Date leftEndDate = null;  
	        Date rightStartDate = null;  
	        Date rightEndDate = null;  
	         
	        leftStartDate = parseDate(startdate1);  
	        leftEndDate = parseDate(enddate1);  
	        rightStartDate = parseDate(startdate2);  
	        rightEndDate = parseDate(enddate2);  
	        
	        if(leftStartDate==null || leftEndDate==null || rightStartDate==null || rightEndDate==null){
	        	return false;
	        }
	          
	        return   
	            ((leftStartDate.getTime() >= rightStartDate.getTime())   
	                    && leftStartDate.getTime() < rightEndDate.getTime())  
	            ||  
	            ((leftStartDate.getTime() > rightStartDate.getTime())   
	                    && leftStartDate.getTime() <= rightEndDate.getTime())  
	            ||  
	            ((rightStartDate.getTime() >= leftStartDate.getTime())   
	                    && rightStartDate.getTime() < leftEndDate.getTime())  
	            ||  
	            ((rightStartDate.getTime() > leftStartDate.getTime())   
	                    && rightStartDate.getTime() <= leftEndDate.getTime());  
	              
	    }  
		

		public static boolean isOverlapTime(String startTime1, String endTime1,  
	            String startTime2, String endTime2) {  
			Date leftStartDate = null;  
			Date leftEndDate = null;  
			Date rightStartDate = null;  
			Date rightEndDate = null;   
	        
	        
	        leftStartDate = parseDate(getDate()+" "+ startTime1);  
	        leftEndDate = parseDate(getDate()+" "+ endTime1);  
	        rightStartDate = parseDate(getDate()+" "+ startTime2);  
	        rightEndDate = parseDate(getDate()+" "+ endTime2);  
	        
	        if(leftStartDate==null || leftEndDate==null || rightStartDate==null || rightEndDate==null){
	        	return false;
	        }
	          
	        return   
	            ((leftStartDate.getTime() >= rightStartDate.getTime())   
	                    && leftStartDate.getTime() < rightEndDate.getTime())  
	            ||  
	            ((leftStartDate.getTime() > rightStartDate.getTime())   
	                    && leftStartDate.getTime() <= rightEndDate.getTime())  
	            ||  
	            ((rightStartDate.getTime() >= leftStartDate.getTime())   
	                    && rightStartDate.getTime() < leftEndDate.getTime())  
	            ||  
	            ((rightStartDate.getTime() > leftStartDate.getTime())   
	                    && rightStartDate.getTime() <= leftEndDate.getTime());  
		}
}
